package com.tsd.jwt;

import com.tsd.core.constants.JwtConstants;
import com.tsd.core.jwt.AccessToken;
import com.tsd.core.jwt.JwtUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.system.entity.SysUserExt;
import io.jsonwebtoken.Claims;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName: JwtRefreshUtil
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2021/1/12 14:21
 * @Version: 1.0
 */
public class JwtRefreshUtil {

    private static final ConcurrentHashMap<String, String> CACHE_TOKEN_MAP;
    private static final ConcurrentHashMap<String, String> REFRESH_TOKEN_MAP;

    static {
        CACHE_TOKEN_MAP = new ConcurrentHashMap<>();
        REFRESH_TOKEN_MAP = new ConcurrentHashMap<>();
    }

    public static void addCacheToken(String token, SysUserExt opUser) {
//        addCacheToken(token, opUser, null);
    }

    public static void addCacheToken(String token, SysUserExt opUser, Long expired) {
        if (expired == null) {
            expired = JwtUtils.getTokenExpiredMs();
        }
        CACHE_TOKEN_MAP.put(token, token);
        createRefreshToken(token, opUser, expired);
    }

    private static void createRefreshToken(String token, SysUserExt opUser, Long expired) {
        if (expired == null) {
            expired = JwtUtils.getTokenExpiredMs();
        }
        String refreshToken = JwtHelper.createJWT(JwtConstants.TYPE_SYS_USER, opUser, expired * 2, JwtUtils.getEncodingAesKey());
        REFRESH_TOKEN_MAP.put(token, refreshToken);
    }

    public static Claims getToken(String token) throws HlpException {
//        String cacheToken = CACHE_TOKEN_MAP.get(token);
//        Claims claims = JwtHelper.parseJWT(cacheToken, JwtHelper.getEncodingAesKey());
//        if (claims == null) {
//            claims = JwtHelper.parseJWT(REFRESH_TOKEN_MAP.get(token), JwtHelper.getEncodingAesKey());
//            CACHE_TOKEN_MAP.put(token, REFRESH_TOKEN_MAP.get(token));
//            //创建新的刷新Token
//            SysUserExt userExt = SysUtil.getSysUserFromClaims(claims);
//            createRefreshToken(token, userExt, JwtHelper.getTokenExpiredSeconds());
//        }
//        return claims;
        return JwtUtils.parseJWT(token, JwtUtils.getEncodingAesKey());
    }

    public static void createSysUserToken(SysUserExt userExt) throws HlpException {
        createSysUserToken(JwtConstants.TYPE_SYS_USER, userExt);
    }

    public static void createSysUserToken(String type, SysUserExt userExt) throws HlpException {
        //拼装accessToken
        String token = JwtHelper.createJWT(type, userExt);
        JwtRefreshUtil.addCacheToken(token, userExt);
        //返回accessToken
        AccessToken accessToken = new AccessToken();
        accessToken.setToken(token);
        accessToken.setExpires_in(JwtUtils.getTokenExpiredSeconds());
        userExt.setAccessToken(accessToken);
        //刷新token
        String refreshTokenStr = JwtHelper.createJWT(type, userExt, JwtUtils.getTokenExpiredMs() * 10);
        AccessToken refreshToken = new AccessToken();
        refreshToken.setToken(refreshTokenStr);
        refreshToken.setExpires_in(JwtUtils.getTokenExpiredSeconds() * 10);
        userExt.setRefreshToken(refreshToken);
    }
}
